package com.googlecode.jvmpart.proc.regex;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.googlecode.jvmpart.proc.AbsProcessor;
import com.googlecode.jvmpart.proc.JvmProcessException;

public abstract class RegexProcessor extends AbsProcessor {
	private String content;
	public String getContent() {
		return content;
	}
	public void doExecute()
			throws JvmProcessException {
		Matcher m = null;
		content = regexContent(getParams());
		m = regexPattern(getParams()).matcher(content);
		int offset = 0;
		while (m.find()) {
			try {
				boolean process = matchProcess(m, offset++);
				if(!process) break;
			} catch(JvmProcessException e) {
				if(whenError(e)) throw e;				
			}
		}		
		completed();
	}
	public abstract boolean matchProcess(Matcher m, int offset)throws JvmProcessException;
	public abstract Pattern regexPattern(Map<String, String> params);
	public abstract String regexContent(Map<String, String> params) throws JvmProcessException;
	public abstract boolean whenError(Throwable e);
	public abstract void completed() throws JvmProcessException;

}
